﻿using System;
using System.Windows.Forms;
using bbtpClient.Entities;
using bbtpClient.Controllers;
using System.Text.RegularExpressions;

namespace bbtpClient
{
    public partial class AddEditTransportCompanyForm : Form
    {
        private TransportCompany transportCompany;
        public AddEditTransportCompanyForm(string transporCompanyName, TransportCompany transportCompany)
        {
            InitializeComponent();
            label_UserName.Text = UserInfo.Name;
            this.transportCompany = transportCompany;
            if (transportCompany.id != 0)
            {
                AddTransportCompanyDataToForm();
            }
            Text = transporCompanyName;
        }

        private void AddTransportCompanyDataToForm()
        {
            textBox_companyName.Text = transportCompany.name;
            textBox_companyEmail.Text = transportCompany.email;
            textBox_companyAddress.Text = transportCompany.address;
            textBox_companyPhone.Text = transportCompany.phoneNumber;
        }

        private bool IsDataCorrect()
        {
            bool isNameCorrect = false;
            bool isEmailCorrect = false;
            bool isAddressCorrect = false;
            bool isPhoneCorrect = false;

            if (textBox_companyName.Text.Trim() != "")
            {
                isNameCorrect = true;
            }
            if (textBox_companyEmail.Text.Trim() != "")
            {
                string pattern = @"\w+\@\w+\.\w+";
                Match match = Regex.Match(textBox_companyEmail.Text.Trim(), pattern);
                if (match.Success)
                {
                    isEmailCorrect = true;
                }
            }
            if (textBox_companyAddress.Text.Trim() != "")
            {
                isAddressCorrect = true;
            }
            if (textBox_companyPhone.Text.Trim() != "")
            {
                isPhoneCorrect = true;
            }

            return isNameCorrect && isEmailCorrect && isAddressCorrect && isPhoneCorrect;
        }

        private void AddDataFromFormToTransportCompany()
        {
            transportCompany.name = textBox_companyName.Text;
            transportCompany.email = textBox_companyEmail.Text;
            transportCompany.address = textBox_companyAddress.Text;
            transportCompany.phoneNumber = textBox_companyPhone.Text;
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            if (IsDataCorrect())
            {
                AddDataFromFormToTransportCompany();
                TransportManagerController.SaveTransportCompany(transportCompany, this);
            }
            else
            {
                MessageBox.Show("Пожалуйста, введите все данные корректно");
            }
            
        }
    }
}